/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

/**
 * [getObjById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Object]}      [对象]
 */
export const getObjById = (data, id) => {
    if(!data || !data.length || !id) return {};
    const newData = [];
    for(let i = 0; i < data.length; i++) {
        /*eslint-disable  eqeqeq*/
        if(data[i].id == id)
            return data[i];
        if(data[i].items)
            newData.push(...data[i].items);
    }
    return getObjById(newData, id);
};

/**
 * [getPathById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Array]}      [path]
 */
export const getPathById = (data, id) => {
    for(let i = 0; i < data.length; i++) {
        const organization = data[i];
        /*eslint-disable  eqeqeq*/
        if(organization.id == id)
            return [i];
        if(organization.items && organization.items.length > 0) {
            const path = getPathById(organization.items, id);
            if(path)
                return [i, 'items'].concat(path);
        }
    }
};

export const getTreeData = (data, props, path) => {
    const newData = [...data];
    const obj = path.reduce((a, item) => a[item], newData);
    obj.items = props;
    return newData;
};

import {formatMessage} from './intl';
export const validateAdd = (data, id) => {
    const {
        brandId,
        dealerId,
        typeId,
        deliveryCycle,
        amountRate,
        kindRate,
        feeRate,
    } = data;
    const msgs = [];
    if(!id && !brandId)
        msgs.push(
            formatMessage({
                id: 'utils.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!id && !dealerId)
        msgs.push(
            formatMessage({
                id: 'utils.dealerId',
                defaultMessage: '总代ERP号',
            })
        );
    if(!id && !typeId)
        msgs.push(
            formatMessage({
                id: 'utils.typeId',
                defaultMessage: '订单类型',
            })
        );
    if(!amountRate)
        msgs.push(
            formatMessage({
                id: 'utils.amountRate',
                defaultMessage: '数量满足率',
            })
        );
    if(!kindRate)
        msgs.push(
            formatMessage({
                id: 'utils.kindRate',
                defaultMessage: '品种满足率',
            })
        );
    if(!feeRate)
        msgs.push(
            formatMessage({
                id: 'utils.feeRate',
                defaultMessage: '金额满足率',
            })
        );
    if(!deliveryCycle)
        msgs.push(
            formatMessage({
                id: 'utils.deliveryCycle',
                defaultMessage: '交付周期',
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空或等于0'
        })}`;
    return false;
};
